# -*- coding: UTF-8 -*-
__author__ = 'czyigy'
import binascii
from constant import *
import struct
from datetime import datetime as dt_date_time
import traceback
from logger import logger
from constant import get_alarm_info

class ENVMonitorUnPack():
    def __init__(self):

        self.envmonitorInfo ={
            'humidity': 0,
            'temperature': 0,
            'PM100': 0,
            'noise': 0,
            'formaldehyde': 0,
            'PM25': 0,
            'PM10': 0,
            'atmospressue': 0,
            'illumination': 0,
            }

        self.addr = 0x19
        self.VerifyCode = 0
        self.function_code = 0
        self.data_len = 0
        self.all_data = {}

    def init_data(self):
        self.envmonitorInfo = {
            'humidity': 0,
            'temperature': 0,
            'PM100': 0,
            'noise': 0,
            'formaldehyde': 0,
            'PM25': 0,
            'PM10': 0,
            'atmospressue': 0,
            'illumination': 0,
            }

        self.addr = 0x19
        self.function_code = 0
        self.data_len = 0
        self.VerifyCode = 0

        self.all_data = {}

    #---------------basic function-----------------#
    def BYTEToString(self, src):
        return str(binascii.b2a_hex(src))

    def BYTEToInt(self, src):
        return int(binascii.b2a_hex(src), 16)

    def WordToInt(self, src):
        return int(binascii.b2a_hex(src), 16)

    def DWordToInt(self, src):
        return int(binascii.b2a_hex(src), 16)

    def decode_humidity(self, src):
        humidity = self.WordToInt(src)
        return round(humidity/10.0, 1)

    def decode_temperature(self, src):
        temperature = self.WordToInt(src)
        return round(temperature/10.0, 1)

    def decode_noise(self, src):
        noise = self.WordToInt(src)
        return round(noise/10.0, 1)

    def decode_pm25(self, src):
        pm25 = self.WordToInt(src)
        return pm25

    def decode_pm10(self, src):
        pm10 = self.WordToInt(src)
        return pm10

    def decode_atmospressue(self, src):
        atmospressue = self.DWordToInt(src)
        return round(atmospressue /1000.0, 3)

    def decode_env_monitor(self, src):
        try:
            self.envmonitorInfo['humidity'] = self.decode_humidity(src[0:2])
            self.envmonitorInfo['temperature'] = self.decode_temperature(src[2:4])
            self.envmonitorInfo['PM100'] = self.WordToInt(src[4:6])
            self.envmonitorInfo['noise'] = self.decode_noise(src[6:8])
            self.envmonitorInfo['formaldehyde'] = self.WordToInt(src[8:10])
            self.envmonitorInfo['PM25'] = self.decode_pm25(src[10:12])
            self.envmonitorInfo['PM10'] = self.decode_pm10(src[12:14])
            self.envmonitorInfo['atmospressue'] = self.decode_atmospressue(src[14:18])
        except:
            return False
        return True

    def decode_all_data(self, src):
        self.init_data()
        try:
            error_msg = "unknown"
            self.addr = self.BYTEToInt(src[0])
            self.function_code = self.BYTEToInt(src[1])
            self.data_len = self.BYTEToInt(src[2])
            data_end = self.data_len + 3
            if self.addr == 0x19 and self.function_code == 0x03:

                self.decode_env_monitor(src[3:data_end])

                self.all_data = {
                    'time': dt_date_time.now().strftime('%Y-%m-%d %H:%M:%S'),
                    'addr': self.addr,
                    'function_code': self.function_code,
                    'data_len': self.data_len,
                    'VerifyCode': self.VerifyCode,
                    'envmonitorInfo': self.envmonitorInfo
                }
        except:
            error_msg = "decode error, length of buffer is not enough"
            logger.error(error_msg)
            return False, error_msg
        return True, 'success'



